package module.main.view.mainInterface.troopListPanel
{
	import com.darwin.darwinUI.controls.Button;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.controls.List;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.events.UIEvent;
	import com.darwin.darwinUI.general.DarwinFont;
	import com.darwin.darwinUI.general.TextFieldFactory;
	import com.darwin.darwinUI.manager.UIManager;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	
	import module.main.view.ResourceConst;
	
	/**
	 * 部队列表
	 * @author killsoon
	 */	
	public class TroopListPanel extends UIComponent
	{
		private var _data:Array;
		private var panelBg:UIComponent;
		private var leftArrow:Button;
		private var rightArrow:Button;
		private var troopList:List;
		private var pageTxt:Label;
		
		
		
		
		public function TroopListPanel()
		{
			super();
			initView();
			initListener();
			
		}
		
		private function initView():void
		{
			drawBg();
			createArrow();
			createPageIndex();
			createList();
		}
		
		private function initListener():void
		{
			troopList.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
			troopList.addEventListener(UIEvent.LIST_ROLL_COMP, rollPageComplete);
			addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
			leftArrow.addEventListener(MouseEvent.CLICK, changePage);
			rightArrow.addEventListener(MouseEvent.CLICK, changePage);
		}
		
		private function drawBg():void
		{
			panelBg = new UIComponent();
			panelBg.backgroundSkin = ResourceConst.TROOPLIST_BG;
			panelBg.backgroundAuto = true;
			addChild(panelBg);
		}
		/**
		 * 列表添加到舞台
		 */		
		private function addedToStage(e:Event):void
		{
			rollPageComplete();
		}
		
		/**
		 * 从舞台上移除面板
		 */		
		private function removedFromStage(e:Event):void
		{
			removeAllChild();
		}
		
		/**
		 *	翻页 
		 */		
		private function changePage(e:MouseEvent):void
		{
			switch(e.currentTarget)
			{
				case leftArrow:
					troopList.rollToPage(troopList.currentPage - 1, 0);
					break;
				case rightArrow:
					troopList.rollToPage(troopList.currentPage + 1, 0);
					break;
				default:
					break;
			}
		}
		
		/**
		 *	翻页结束改变当前页数 
		 */		
		private function rollPageComplete(e:UIEvent = null):void
		{
			if(troopList.pageLength == 0)
			{
				pageTxt.text = UIManager.getLanguageManager().getText("ForceList_page", [0, 0]);
			}
			else
			{
				pageTxt.text = UIManager.getLanguageManager().getText("ForceList_page", [troopList.currentPage + 1, troopList.pageLength]);
			}
			
			pageTxt.x = 41 + (67 - (pageTxt.width + 5)) / 2;
			
			if(troopList.currentPage != 0 && troopList.currentPage != troopList.pageLength - 1)
			{
				leftArrow.filters = [];
				rightArrow.filters = [];
				leftArrow.mouseChildren = true;
				rightArrow.mouseChildren = true;
				leftArrow.mouseEnabled = true;
				rightArrow.mouseEnabled = true;
			}
			else
			{
				if(troopList.currentPage == 0 && (troopList.currentPage == troopList.pageLength - 1 || troopList.pageLength == 0))
				{
					leftArrow.filters = [new ColorMatrixFilter([0.3086, 0.6094, 0.082, 0, 0, 0.3086, 
						0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0])];
					leftArrow.mouseChildren = false;
					leftArrow.mouseEnabled = false;
					rightArrow.filters = [new ColorMatrixFilter([0.3086, 0.6094, 0.082, 0, 0, 0.3086,
						0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0])];
					rightArrow.mouseChildren = false;
					rightArrow.mouseEnabled = false;
				}
				else
				{
					if(troopList.currentPage == 0)
					{
						//	添加黑白滤镜
						leftArrow.filters = [new ColorMatrixFilter([0.3086, 0.6094, 0.082, 0, 0, 0.3086, 
							0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0])];
						rightArrow.filters = [];
						leftArrow.mouseChildren = false;
						leftArrow.mouseEnabled = false;
						rightArrow.mouseChildren = true;
						rightArrow.mouseEnabled = true;
					}
					else
					{
						rightArrow.filters = [new ColorMatrixFilter([0.3086, 0.6094, 0.082, 0, 0, 0.3086,
							0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0])];
						leftArrow.filters = [];
						leftArrow.mouseChildren = true;
						leftArrow.mouseEnabled = true;
						rightArrow.mouseChildren = false;
						rightArrow.mouseEnabled = false;
					}
				}
			}
		}
		
		/**
		 * 	创建左右翻页按钮
		 */		
		private function createArrow():void
		{
			leftArrow = new Button();
			leftArrow.backgroundSkin = ResourceConst.LEFT_ARROW;
			leftArrow.backgroundAuto = true;
			leftArrow.x = 26;
			leftArrow.y = 222;
			addChild(leftArrow);
			
			rightArrow = new Button();
			rightArrow.backgroundSkin = ResourceConst.LEFT_ARROW;
			rightArrow.backgroundAuto = true;
			rightArrow.mouseChildren = false;
			rightArrow.scaleX = -1;
			rightArrow.x = 113;
			rightArrow.y = 222;
			addChild(rightArrow);
		}
		/**
		 * 	创建部队列表
		 */		
		private function createList():void
		{
			troopList = new List(TroopListItem, 125, 41, 0, 2);
			troopList.width = 125;
			troopList.height = 250;
			troopList.x = 7;
			troopList.y = 8;
			addChild(troopList);
		}
		
		/**
		 *	创建页数显示 
		 */		
		private function createPageIndex():void
		{
			pageTxt = TextFieldFactory.getInstance().getLabel("ForceList_page");
			pageTxt.mouseEnabled = false;
			pageTxt.y = 223;
			pageTxt.autoSize = true;
			pageTxt.x = (67 - (pageTxt.width + 5)) / 2;
			addChild(pageTxt);
		}
		
		public function set data(value:Array):void
		{
			_data = value;
			if(value)
			{
				troopList.source = _data;
			}
			else
				troopList.source = [];
		}
		
		public function get data():Array
		{
			return _data;
		}
	}
}